package one.stand.controller;

import com.jmp.api.crm.dto.BannerEditRequest;
import com.jmp.api.crm.dto.BannerRemoveRequest;
import com.jmp.api.crm.dto.BannerRequest;
import com.jmp.api.crm.dto.BannerResponse;
import com.jmp.autocofiguration.web.interceptor.SecurityAuthCheck;
import com.jmp.feign.BannerFeign;
import com.jmp.service.api.admin.BannerApi;
import com.jmp.service.api.admin.dto.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import one.stand.dto.ConvertUtils;
import one.stand.dto.PageResponse2;
import one.stand.model.Goods;
import one.stand.model.ResultModel;
import one.stand.service.GoodsBaseService;
import one.stand.util.AssertUtil;
import one.stand.util.RequestContext;
import one.stand.util.RequestContextHolder;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * Created by chengjiarui on 2019-06-27.
 */
@RestController
@RequestMapping("crmPc/banner")
@Api("企业轮播图管理")
public class BannerController {
    @Autowired
    private BannerFeign bannerFeign;

    @Autowired
    private GoodsBaseService goodsBaseService;

    @ApiOperation("轮播图列表")
    @GetMapping("")
    @SecurityAuthCheck(company = true, card = true)
    public ResultModel<BannerResponse> list(@RequestAttribute BannerRequest request) {
        BannerListDto bannerListDto = BannerListDto.builder()
                .companyId(request.getCId())
                .bannerType(request.getType())
                .refId(null)
                .build();
        bannerListDto.setPageInit(request.getPageNum(), request.getPageSize());
        ResultModel<BannerListVo> bannerListVoResultModel = bannerFeign.list(bannerListDto);
        AssertUtil.isTrue(bannerListVoResultModel);
        PageResponse2<BannerResponse.Banner> banners = ConvertUtils.page2(bannerListVoResultModel.getData().getBannerPage(), companyBanner -> {
            BannerResponse.Banner banner = BannerResponse.Banner.builder()
                    .id(companyBanner.getId())
                    .source(companyBanner.getSource())
                    .type(companyBanner.getType())
                    .event(companyBanner.getEvent())
                    .eventId(companyBanner.getEventId())
                    .sort(companyBanner.getSort())
                    .createTime(DateFormatUtils.ISO_8601_EXTENDED_DATETIME_FORMAT.format(companyBanner.getCreateTime()))
                    .selected(Objects.nonNull(companyBanner.getSelectBannerId()))
                    .status(BannerApi.STATUS_UP == companyBanner.getStatus())
                    .build();

            if (BannerApi.EVENT_GOODS == banner.getEvent()) {
                Goods goodsModel = goodsBaseService.get(Integer.valueOf(companyBanner.getEventId()));
                if (Objects.nonNull(goodsModel)) {
                    banner.setEventIdName(goodsModel.getGoodsName());
                }
            }
            return banner;
        });

        BannerResponse response = new BannerResponse();
        response.setBanners(banners);
        return ResultModel.success(response);
    }

    @ApiOperation("添加或更新轮播图")
    @PostMapping("edit")
    @SecurityAuthCheck(company = true, card = true)
    public ResultModel edit(@RequestAttribute BannerEditRequest request) {

        final RequestContext.User optUser = RequestContextHolder.getRequestContext().getUser();

        ResultModel resultModel = bannerFeign.save(BannerSaveDto.builder()
                .id(request.getBannerId())
                .bannerType(request.getBannerType())
                .companyId(optUser.getCompanyId())
                .source(request.getSource())
                .type(request.getType())
                .event(request.getEvent())
                .eventId(request.getEventId())
                .sort(request.getSort())
                .build());
        AssertUtil.isTrue(resultModel);

        return ResultModel.success();
    }

    @ApiOperation("删除轮播图")
    @PostMapping("remove")
    @SecurityAuthCheck(company = true, card = true)
    public ResultModel remove(@RequestAttribute BannerRemoveRequest request) {

        ResultModel resultModel = bannerFeign.remove(BannerRemoveDto.builder().id(request.getBannerId()).build());
        AssertUtil.isTrue(resultModel);

        return ResultModel.success();
    }


    @ApiOperation("上架")
    @PostMapping("up")
    @SecurityAuthCheck(company = true, card = true)
    public ResultModel up(@RequestAttribute BannerRemoveRequest request) {

        ResultModel resultModel = bannerFeign.up(BannerUpDto.builder().id(request.getBannerId()).build());
        AssertUtil.isTrue(resultModel);

        return ResultModel.success();
    }

    @ApiOperation("下架")
    @PostMapping("down")
    @SecurityAuthCheck(company = true, card = true)
    public ResultModel down(@RequestAttribute BannerRemoveRequest request) {

        ResultModel resultModel = bannerFeign.down(BannerDownDto.builder().id(request.getBannerId()).build());
        AssertUtil.isTrue(resultModel);

        return ResultModel.success();
    }
}
